package com.blockmeta.bbs.immodule.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.adapter.RecommandUserAdapter;
import com.blockmeta.bbs.immodule.bean.databean.UserBean;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.corebean.IUserDomainService;
import com.blockmeta.bbs.immodule.net.api.RecommandUserApi;
import com.blockmeta.bbs.immodule.widget.IMAddFriendMsg;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author airthor
 * @date 2019/5/15
 *
 * 推荐用户
 */
public class RecommandUserActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R2.id.rcv_recommanduser_list)
    RecyclerView mRcvFansList;

    @BindView(R2.id.tv_search_cancel)
    TextView mTvSearchCancel;

    @BindView(R2.id.edt_search_input)
    TextView mEdtSearchInput;

    private List<UserBean> mRecommandUsers = new ArrayList<>();
    private RecommandUserAdapter mAdapter;

    private IUserDomainService mIUserRepo;
    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_recommanduser,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        setTitleText("添加好友");
        mIUserRepo = IMApplicationContext.getUserDomainService();
        initView();
        network();
    }

    private void network() {
        RetrofitFactory.getRetrofit().create(RecommandUserApi.class)
                .getRecommendUsers(AccountHelper.getUidInt())
                .compose(this.<BaseImResponse<List<UserBean>>>bindToLifecycle())
                .compose(RxUtil.<List<UserBean>>commonResult())
                .compose(RxUtil.<List<UserBean>>rxSchedulerHelper())
                .subscribe(new CommonSubscriber<List<UserBean>>(mContext) {
                    @Override
                    public void onNext(List<UserBean> list) {
                        for (UserBean userBean : list) {
                            userBean.setFriend(mIUserRepo.isFriend(AccountHelper.getUidInt(),userBean.getId()));
                        }
                        mRecommandUsers.addAll(list);
                        mAdapter.setNewData(mRecommandUsers);

                        if (mAdapter.getEmptyView()==null) {
                            mAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_data,null));
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        if (mAdapter.getEmptyView()==null) {
                            mAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_data,null));
                        }
                    }
                });
    }
    private void initView() {
        mRcvFansList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new RecommandUserAdapter(R.layout.item_recommanduser,mRecommandUsers);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                int i = view.getId();
                if (i == R.id.btn_addfriend) {
                    final UserBean bean = mAdapter.getData().get(position);
                    if (bean != null) {
                        IMAddFriendMsg.OnRequestSuccessListiener requestSuccessListiener = new IMAddFriendMsg.OnRequestSuccessListiener() {
                            @Override
                            public void requestSuccess() {
                                bean.setHasBeenSentFriendRequest(true);
                                mAdapter.notifyItemChanged(position);
                            }
                        };
                        IMAddFriendMsg imPopAddFriendMsg = new IMAddFriendMsg(mContext, bean.getId() + "",requestSuccessListiener);
                        imPopAddFriendMsg.show();
                    }

                } else {
                }
            }
        });
        mAdapter.bindToRecyclerView(mRcvFansList);
        mAdapter.setOnItemClickListener(this);
    }

    @OnClick({R2.id.edt_search_input})
    public void onViewClicked(View view) {
        Intent intent = new Intent(mContext,SearchFriendActivity.class);
        startActivity(intent);
        finish();
    }
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UserBean data= (UserBean) adapter.getData().get(position);
        RouterHelper.startActivity(mContext, RouterPath.PERSONHOME,String.valueOf(data.getId()));
    }

    @Override
    public boolean showRight() {
        return true;
    }

    @Override
    public int showRightImg() {
        return R.drawable.title_bar_search;
    }

    @Override
    public void toDoRightSomething() {
        Intent intent = new Intent(mContext,SearchFriendActivity.class);
        startActivity(intent);
        finish();
    }
}
